package main

import (
	"fmt"
	"os"
	"strings"

	"fyne.io/fyne"
	"fyne.io/fyne/app"
	"fyne.io/fyne/theme"
	"fyne.io/fyne/widget"
	"github.com/atotto/clipboard" // 操作剪切板
	"github.com/flopp/go-findfont"
)

var (
	myApp         fyne.App
	usernameEny   *widget.Entry
	passwordEny   *widget.Entry
	loginResLabel = widget.NewLabel("")
	loginRemember bool
)

func setFront() {
	//获取中文字体列表
	var fontPaths = findfont.List()
	for _, path := range fontPaths {
		//设置字体
		if strings.Contains(path, "STHeiti Light.ttc") {
			_ = os.Setenv("FYNE_FONT", path)
			break
		}
	}
}

func login(username, password string) {
	if username == "admin" {
		if password == "123123" {
			loginResLabel.SetText("登录成功")
			fmt.Println(loginRemember)
		} else {
			loginResLabel.SetText("密码错误")
		}
	} else {
		loginResLabel.SetText("用户不存在")
	}
}

func main() {
	setFront()
	myApp = app.New()

	// 创建新窗口
	myWin := myApp.NewWindow("Hello")

	// 添加工具栏
	icon1 := theme.ContentCutIcon()
	toolbar := widget.NewToolbar(
		widget.NewToolbarAction(icon1, func() {
			fmt.Println(icon1.Name())
			_ = clipboard.WriteAll(fmt.Sprintf("username: %s , password: %s", usernameEny.Text, passwordEny.Text))
		}),
	)

	// 创建盒子控件
	content := widget.NewVBox()
	content.Append(toolbar)

	// 创建label
	lb1 := widget.NewLabel("登录页")

	// 添加label到盒子
	content.Append(lb1)

	// 创建Entry
	usernameEny = widget.NewEntry()
	eny1 := widget.NewVBox(
		widget.NewLabel("用户名"),
		usernameEny,
	)
	content.Append(eny1)

	// 创建Entry
	passwordEny = widget.NewPasswordEntry()
	eny2 := widget.NewVBox(
		widget.NewLabel("密码"),
		passwordEny,
	)
	content.Append(eny2)

	// 创建选择框
	chk1 := widget.NewCheck("记住密码", func(b bool) {
		loginRemember = b
	})
	content.Append(chk1)

	// 创建Button
	btn1 := widget.NewButton("登录", func() {
		// 获取Entry值
		login(usernameEny.Text, passwordEny.Text)
	})
	content.Append(btn1)
	content.Append(loginResLabel)

	// 添加盒子到窗口
	myWin.SetContent(content)

	// 设置窗口大小
	myWin.Resize(fyne.NewSize(200, 200))

	// 显示窗口，并进入循环
	myWin.ShowAndRun()

	_ = os.Unsetenv("FYNE_FONT")
}
